from sanic import response

from deviceModel import search_user_link_dev, search_dev, insert_user_link_dev, delete_user_link_dev, update_dev
from salesmanModel import search_user_link_role, insert_user_link_role, delete_user_link_role, judge_a_staff
from userModel import select, update, insert


# ###########################
# ...|——销售人员管理          #
#       |——编辑销售人员       #
#       |——新增销售人员       #
# ###########################
# 销售人员管理界面> 返回所有销售人员列表


async def salesman_management(request):
        # 获取公司名字
        company_id = request.form.get('company_id')
        # 获取销售人员列表
        salesman_info = get_staff_list(company_id=company_id)
        return response.json(salesman_info)


# 添加销售人员
async def add_salesman(request):
        name = request.form.get('salesman_name')
        tel = request.form.get('salesman_tel')
        company_id = request.form.get('company_id')

        user_exist = select(tel=tel)
        id = user_exist['id']
        if user_exist:
                u_l_r_exist = search_user_link_role(company_id=company_id, role_id=6, user_id=id)
                if u_l_r_exist:
                        print('重复添加！')
                        return response.text('add fail!')
                else:
                        print('修改添加')
                        insert_user_link_role(user_id=id, role_id=6, company_id=company_id)
                        update(tel=tel, new_username=name)
                        return response.text('add success!')
        else:
                print('创建添加')
                insert_user_link_role(user_id=id, role_id=6, company_id=company_id)
                insert(tel=tel, username=name, passwd=tel[-6:])
                return response.text('add success!')


# 编辑销售人员界面> 接收人员姓名，手机号
async def edit_salesman(request):
        # 获取编辑的旧信息，新信息
        oldname = request.form.get('old_salesman_name')         # 是否需要旧名字？
        oldtel = request.form.get('old_salesman_tel')
        name = request.form.get('salesman_name')
        tel = request.form.get('salesman_tel')

        edit_success = core_edit_salesman(old_name=oldname, oldtel=oldtel, name=name, tel=tel)
        if edit_success:
                return response.text('edit Success!')
        else:
                return response.text('edit Fail!')


async def delete_salesman(request):
        company_id = request.form.get('company_id')
        tel = request.form.get('salesman_tel')
        user_id = select(tel=tel)['id']

        exist = search_user_link_role(company_id=company_id, role_id=6, user_id=user_id)
        if exist:
                success = delete_user_link_role(company_id=company_id, role_id=6, user_id=user_id)
                if success:
                        return response.text('delete success!')
                else:
                        print('数据库错误！')
        else:
                print('该条不存在！')
        return response.text('delete fail!')


# 获取人员列表，默认销售人员（role_id=6）
def get_staff_list(company_id, role_id=6):
        # 找到该公司的P级员工(6-P-卖家销售)
        P = search_user_link_role(company_id=company_id, role_id=role_id)
        user_id_list = [P[i]['user_id'] for i in range(len(P))]  # 取出u_l_r表中的user_id

        people_info = []  # 返回列表
        for i in range(len(user_id_list)):
                # 对着user_id查user表
                people = select(id=user_id_list[i])
                username = people['username']
                tel = people['tel']
                id = people['id']
                add = {'id': id, 'username': username, 'tel': tel}
                people_info.append(add)  # 返回列表：[{}, {}]
        return people_info


def core_edit_salesman(old_name, oldtel, name, tel):
        if name and tel:      # 如果修改不为空
                if oldtel != tel or old_name != name:   # 如果新旧数据不是一样的
                        success = update(tel=oldtel, new_username=name, new_tel=tel)
                        if success:
                                print('修改成功！')
                                return True
                        else:
                                print('修改失败！')
                                return False
                else:
                        print('用户输入相同信息！')
                        return False
        else:
                print('用户输入为空！')
                return False

# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<销售人员的设备管理


# #################################
# 主页面-销售人员                    #
#       |——设备演示                 #
#       |   |——新增设备             #
#       |   |——参数设置             #
# （这里的个人信息和卖家演示该怎么放）    #
# #################################
class salesman(object):

        def __init__(self):
                self.name = ''
                self.phone = -1
                self.display_device = []    # 设备号

        def get_saelesman_info(self, id):
                # 根据id获取信息，这里模拟获取信息
                self.name = 'Ralph'
                self.phone = 15342344775
                self.display_device = ['19012001', '19012002']

                info = {'name': self.name,
                        'phone': self.phone,
                        'device_id': self.display_device}
                return info

        def delete_display_device(self, salesman_id, device_id):
                # 修改其拥有的设备号
                return True


you = salesman()


async def salesman_dev_display(request):
        user_id = request.form.get('user_id')

        # 获取salesman手下的设备
        u_l_d = search_user_link_dev(user_id=user_id)
        dev_ids = []
        for i in u_l_d:
                dev_ids.append(i['device_id'])

        # 获取设备信息
        dev_info = search_dev(device_id=dev_ids)
        return response.json(dev_info)


async def salesman_add_dev(request):
        user_id = request.form.get('user_id')
        device_id = request.form.get('device_id')       # <<<<<<<<<

        # 判断销售人员是否合格
        salesman_exist = judge_a_staff(user_id=user_id, role_id=6)
        if not salesman_exist:
                return response.text('你不是销售人员！')
        # 判断机器是否合格
        dev_exist = search_dev(device_id=device_id, status='B')         # 状态B-->生产已打标
        if not dev_exist:
                return response.text('该机器不符合！')

        # 向u_l_d中添加关系
        # 设备状态:B-->C
        success_u_l_r = insert_user_link_dev(user_id=user_id, device_id=device_id)
        success_dev = update_dev(status='C', device_id=device_id)
        if success_u_l_r and success_dev:
                return response.text('添加成功！')
        else:
                # 回滚
                delete_user_link_dev(user_id=user_id, device_id=device_id)
                update_dev(status='B', device_id=device_id)
                return response.text('添加失败！')   # 返回失败


async def salesman_delete_dev(request):
        user_id = request.form.get('user_id')
        device_id = request.form.get('device_id')

        # 判断销售人员是否合格
        salesman_exist = judge_a_staff(user_id=user_id, role_id=6)
        if not salesman_exist:
                return response.text('你不是销售人员！')        # 非销售人员
        # 判断机器是否合格
        dev_exist = search_dev(device_id=device_id, status='C')         # 状态C-->销售人员绑定
        if not dev_exist:
                return response.text('机器不合理！')   # 机器不合理

        # 删除u_l_d中关系
        # 设备状态:C-->B
        success = delete_user_link_dev(user_id=user_id, device_id=device_id)
        update_dev(status='B', device_id=device_id)
        if success:
                return response.text('删除成功！')
        else:
                # 回滚
                insert_user_link_dev(user_id=user_id, device_id=device_id)
                update_dev(status='C', device_id=device_id)
                return response.text('删除失败！')   # u_l_d中不含此信息


async def salesman_config_stat(request):
        user_id = request.form.get('user_id')
        device_id = request.form.get('device_id')

        # 判断销售人员是否合格
        salesman_exist = judge_a_staff(user_id=user_id, role_id=6)
        if not salesman_exist:
                return response.text('你不是销售人员！')  # 非销售人员
        # 判断机器是否合格
        dev_exist = search_dev(device_id=device_id, status='C')  # 状态C-->销售人员绑定
        if not dev_exist:
                return response.text('机器不合理！')  # 机器不合理

        more_stat = request.form        #<<<<<<<<<<<<<<,5.10

